package common.user;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import api.app.Card;
import api.app.GetAPIAccountInfo;
import api.app.ReportList;
import api.app.Reservation;
import common.subject.Subject;

/**
 * 用户类
 * 定义用户属性和方法
 * @author maojj
 *
 */
public class User
{
	public String token;
	public int id;
	public int pid;
	public String account;
	public String password;
	public String name;
	public short sex;								//性别：1.男；0.女；2.未知
	public short marriage;							//婚姻状况：1.已婚；0.未婚；2.未知
	public String mobile;
	public String voipAccount;
	public String voipPassword;
	public CheckCard[] checkCard;					//已绑定的体检卡
	public CheckReservation[] checkReservation;	//体检预约单
	public CheckReport[] checkReport;				//体检报告
	public Subject subject;						//咨询会话
	
	/**
	 * 用户类构造函数
	 * @param mobile - 手机号
	 * @param password - 密码
	 * @throws JSONException
	 * @throws IOException
	 * @throws ParseException 
	 */
	public User(String mobile, String password) throws JSONException, IOException, ParseException
	{
		setUserInfo(mobile, password);		//初始化用户信息
		setCheckCard(this.token);			//初始化体检卡信息
		setCheckReservation(this.token);	//初始化预约单信息
		setCheckReport(this.token);			//初始化体检报告信息
		setSubject(this.token);				//初始化咨询会话信息
	}
	
	/**
	 * 设置用户属性
	 * @param mobile - 手机号
	 * @param password - 密码
	 * @throws JSONException
	 * @throws IOException
	 */
	public void setUserInfo(String mobile, String password) throws JSONException, IOException
	{
		JSONObject userData = new JSONObject(GetAPIAccountInfo.post(mobile, password));
		this.token = userData.getString("Token");
		this.id = userData.getJSONObject("UserModel").getInt("ID");
		this.pid = userData.getJSONObject("UserModel").getInt("Pid");
		this.account = userData.getJSONObject("UserModel").getString("UserAccount");
		this.password = password;
		this.name = userData.getJSONObject("UserModel").getString("UserName");
		switch (userData.getJSONObject("UserModel").getString("UserSex")){
		case "Man":
			this.sex = 1;
			break;
		case "Woman":
			this.sex = 0;
			break;
		default:
			this.sex = 2;
			break;
		}
		switch (userData.getJSONObject("UserModel").getString("Marriage")) {
		case "Married":
			this.marriage = 1;
			break;
		case "Unmarried":
			this.marriage = 0;
			break;
		default:
			this.marriage = 2;
			break;
		}
		this.mobile = userData.getJSONObject("UserModel").getString("Mobilephone");
		this.voipAccount = userData.getJSONObject("UserModel").getString("voipAccount");
		this.voipPassword = userData.getJSONObject("UserModel").getString("voipPwd");
	}
	
	/**
	 * 设置用户体检卡属性
	 * @param token - token
	 * @throws JSONException
	 * @throws IOException
	 */
	public void setCheckCard(String token) throws JSONException, IOException
	{
		JSONObject checkCardData = new JSONObject(Card.get(this.token));
		JSONArray checkCardArray = checkCardData.getJSONArray("Data");
		int checkCardCount = checkCardArray.length();
		if (checkCardCount > 0)
		{
			this.checkCard = new CheckCard[checkCardCount];
			for (int i=0; i<checkCardCount; i++)
			{
				this.checkCard[i] = new CheckCard();
				this.checkCard[i].number = checkCardArray.getJSONObject(i).getString("Checkcardno");
				this.checkCard[i].ldNumber = checkCardArray.getJSONObject(i).getString("Ldno");
				this.checkCard[i].status = checkCardArray.getJSONObject(i).getString("Status");
				this.checkCard[i].vipFlag = (short) checkCardArray.getJSONObject(i).getInt("VIP");
			}
		}
		else
		{
			this.checkCard = null;
		}
	}
	
	/**
	 * 设置用户体检预约单属性
	 * @param token - token
	 * @throws JSONException
	 * @throws IOException
	 * @throws ParseException 
	 */
	public void setCheckReservation(String token) throws JSONException, IOException, ParseException
	{
		JSONObject checkReservationData = new JSONObject(Reservation.get(this.token));
		JSONArray checkReservationArray = new JSONArray(checkReservationData.getString("Data"));
		int checkReservationCount = checkReservationArray.length();
		if (checkReservationCount > 0)
		{
			this.checkReservation = new CheckReservation[checkReservationCount];
			for (int i=0; i<checkReservationCount; i++)
			{		
				this.checkReservation[i] = new CheckReservation();
				this.checkReservation[i].number = checkReservationArray.getJSONObject(i).getString("ResvNo");
				
				String reserveDate = checkReservationArray.getJSONObject(i).getString("RDate");
				this.checkReservation[i].reserveDate = formatDate(reserveDate, "yyyy-MM-dd'T'HH:mm:ss");
				
				this.checkReservation[i].timePeriodCode = checkReservationArray.getJSONObject(i).getString("RTimeCode");
				this.checkReservation[i].timePeriod = checkReservationArray.getJSONObject(i).getString("RTimeSpan");
				this.checkReservation[i].institutionCode = checkReservationArray.getJSONObject(i).getJSONObject("Institution").getString("Code");
				this.checkReservation[i].institutionName = checkReservationArray.getJSONObject(i).getJSONObject("Institution").getString("Name");
				
				String createDate = checkReservationArray.getJSONObject(i).getString("RequestDate");
				this.checkReservation[i].createDate = formatDate(createDate, "yyyy-MM-dd'T'HH:mm:ss");
			}
		}
		else
		{
			this.checkReservation = null;
		}
	}
	
	/**
	 * 设置用户体检报告
	 * @param token - token
	 * @throws IOException
	 * @throws JSONException
	 */
	public void setCheckReport(String token) throws IOException, JSONException
	{
		JSONObject checkReportData = new JSONObject(ReportList.Options(this.token));
		JSONArray checkReportArray = new JSONArray(checkReportData.getString("Data"));
		int checkReportCount = checkReportArray.length();
		if (checkReportCount > 0)
		{
			this.checkReport = new CheckReport[checkReportCount];
			for (int i=0; i<checkReportCount; i++)
			{		
				this.checkReport[i] = new CheckReport();
				this.checkReport[i].studyId = checkReportArray.getJSONObject(i).getString("StudyId");
				String checkDate = checkReportArray.getJSONObject(i).getString("MedicalDate");
				this.checkReport[i].checkDate = formatDate(checkDate, "yyyy-MM-dd");
			}
		}
		else
		{
			this.checkReport = null;
		}
	}
	
	/**
	 * 设置用户咨询会话
	 * @param token - token
	 * @throws JSONException
	 * @throws IOException
	 * @throws ParseException 
	 */
	public void setSubject(String token) throws JSONException, IOException, ParseException
	{
		JSONObject subjectData = new JSONObject(api.app.Subject.get(this.token));
		if(subjectData.getJSONObject("Data").getString("SubjectInfo")=="null")
		{
			this.subject=null;
			return;
		}
		boolean isActive = subjectData.getJSONObject("Data").getJSONObject("SubjectInfo").getBoolean("IsActive");
		if (isActive)
		{
			Subject subjectInfo=new Subject(token);
			this.subject = subjectInfo;
		}
		else
		{
			this.subject = null;
		}
	}
	
	/**
	 * 将日期格式化为yyyy-MM-dd HH:mm:ss格式
	 * @param sourceDate - 格式化前的日期
	 * @param sourceFormat - 格式化前的日期格式
	 * @return 格式化后的日期；如果执行失败，返回null
	 */
	private String formatDate(String sourceDate, String sourceFormat)
	{
		SimpleDateFormat sdf1 = new SimpleDateFormat(sourceFormat);
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = sdf1.parse(sourceDate);
			String ret = sdf2.format(date);
			return ret;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}


